package demo4;


import java.util.Arrays;
import java.util.Comparator;

//class Student implements Comparable<Student>{
//    public String name;
//    public int age;
//    public double score;
//
//    public Student(String name, int age, double score) {
//        this.name = name;
//        this.age = age;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", score=" + score +
//                '}';
//    }
//
//    @Override
//    public int compareTo(Student o) {
////        if (this.age > o.age){
////            return 1;
////        }else if (this.age == o.age){
////            return 0;
////        }else{
////            return -1;
////        }
////        if (this.name.compareTo(o.name) > 0){
////            return 1;
////        }else if(xxxxxx){
////            return 0;
////        } else{
////            return -1;
////        }
//        //return this.name.compareTo(o.name);
//        //return o.age - this.age;
//        return (int)(this.score-o.score);
//    }
//}

class Student {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
//比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class StringComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zs", 98, 78.9);
        students[1] = new Student("lisi", 48, 18.9);
        students[2] = new Student("abc", 78, 88.9);

        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students, ageComparator);
        System.out.println(Arrays.toString(students));

        StringComparator stringComparator = new StringComparator();
        Arrays.sort(students, stringComparator);
        System.out.println(Arrays.toString(students));

        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students, scoreComparator);
        System.out.println(Arrays.toString(students));
    }

    public static void main3(String[] args) {
        Student student1 = new Student("zhangsan", 38, 78.9);
        Student student2 = new Student("lisi", 38, 18.9);
        //System.out.println(student1.compareTo(student2));
    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zs", 98, 78.9);
        students[1] = new Student("lisi", 48, 18.9);
        students[2] = new Student("abc", 78, 88.9);

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {1,21,4,15,6,17};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
